package com.yhs.YShop.moudle.user.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yhs.YShop.common.utils.R;
import com.yhs.YShop.moudle.user.dto.UserDto;
import com.yhs.YShop.moudle.user.model.User;
import com.yhs.YShop.moudle.user.model.UserLog;
import com.yhs.YShop.moudle.user.service.UserService;

@RequestMapping("user")
@RestController
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * 查询用户的登录日志信息
	 * @return
	 */
	@RequestMapping("selectUserLog")
	public R selectUserLog() {
		Pageable pageable = PageRequest.of(0, 10);
		Page<UserLog> userLogs = userService.findAllUserLog(pageable);
		return R.ok(userLogs.getContent());
	}
	/**
	 * 查询用户的信息
	 * @return
	 */
	@RequestMapping("selectUserInfo")
	public R selectUserInfo() {
		Pageable pageable = PageRequest.of(0, 10);
		Page<User> userLogs = userService.selectPageUserInfo(pageable);
		return R.ok(userLogs.getContent());
	}
	
	@PostMapping("addUser")
	public R addUser(UserDto userDto)
	{
		if (userDto == null) {
			return R.error("用户信息不能为空");
		}
		if (!StringUtils.equals(userDto.getPassWord(), userDto.getRePassWord())) {
			return R.error("密码输入不一致");
		}
		Integer resule = null;
		try {
			resule = userService.addUser(userDto);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (resule != null && resule>0) {
			return R.ok("创建成功");
		}
		return R.error("创建失败");
	}
	
}
